package com.only4play.authcenter.infrastructure.security.providers.admin.sms;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.only4play.authcenter.req.PasswordLoginReq;
import com.only4play.authcenter.req.SmsLoginReq;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;

/**
 * @author hinzzz www.hinzzz.cn 157957329@qq.com
 * 2024-03-05
 */
public class SmsLoginFilter extends AbstractAuthenticationProcessingFilter {
    public SmsLoginFilter(AuthenticationManager manager) {
        super("/auth/smsLogin", manager);
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
        ObjectMapper mapper= new ObjectMapper();
        SmsLoginReq loginReq = mapper.readValue(request.getReader(),SmsLoginReq.class);
        SmsToken token = new SmsToken(Collections.emptyList());
        token.setPhone(token.getPhone());
        token.setVerifyCode(token.getVerifyCode());
        return this.getAuthenticationManager().authenticate(token);
    }
}
